package com.sumainfor.pc.market.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.util.DateUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.market.dao.EpcDataGmfMapper;
import com.sumainfor.pc.market.feign.BaseFeignEpc;
import com.sumainfor.pc.market.feign.BaseFeignZuul;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-05-17 13:49
 * 总经办 工程部
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Log4j2
public class EpcDataGmfService {

    @Autowired
    private EpcDataGmfMapper mapper;

    @Autowired
    private BaseFeignZuul zuul;

    @Autowired
    private BaseFeignEpc epc;

    /**
     * 查询主页表格数据
     *
     * @param areaId    公司编号
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public JsonResult getMainTable(Integer areaId, String startTime, String endTime) {
        List<Map<String, Object>> centList = zuul.getCenterServiceIdList(areaId);
        if (CollectionUtils.isEmpty(centList)) {
            log.error("查询无结果");
            return JsonResult.success(Arrays.asList());
        }
        for (Map<String, Object> dept : centList) {
            Integer deptId = MapUtils.getInteger(dept, "id");
            Map<String, Object> map = epc.getDecoratePlanTableCount(deptId, startTime, endTime);
            dept.putAll(map);
        }
        return JsonResult.success(centList);
    }


    /**
     * 查询子表格数据 装修计划
     *
     * @param page
     * @param miId
     * @param startTime
     * @param endTime
     * @return
     */
    public JsonResult selectPlanForSubTable(Page page, Integer miId, String startTime, String endTime) {
        Page<Map<String, Object>> table = epc.selectPlanForSubTable(page.getSize(), page.getCurrent(), miId, startTime, endTime);
        return JsonResult.success(table);
    }

    /**
     * 查询子表格 装修套数
     *
     * @param page
     * @param miId
     * @param startTime
     * @param endTime
     * @return
     */
    public JsonResult selectHousingForSubTable(Page page, Integer miId, String startTime, String endTime) {
        Page<Map<String, Object>> table = epc.selectHousingForSubTable(page.getSize(), page.getCurrent(), miId, startTime, endTime);
        for (Map<String, Object> map : table.getRecords()) {
            String housingId = MapUtils.getString(map, "HouId");
            if (StringUtils.isBlank(housingId)) {
                return JsonResult.error("获取不到房源编号");
            }
            map.putAll(mapper.selectHousingInfo(housingId));
        }
        return JsonResult.success(table);
    }

    /**
     * 查询装修 所需价格
     *
     * @param page
     * @param miId
     * @param startTime
     * @param endTime
     * @return
     */
    public JsonResult selectCostForSubTable(Page page, Integer miId, String startTime, String endTime) {
        Page<Map<String, Object>> table = epc.selectCostForSubTable(page.getSize(), page.getCurrent(), miId, startTime, endTime);
        return JsonResult.success(table);
    }
}
